package org.hgvc.webapp.ets.jsf.beans.member;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UIParameter;
import javax.faces.component.html.HtmlInputHidden;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.MemberComp;
import org.hgvc.webapp.ets.bo.MemberInd;
import org.hgvc.webapp.ets.bo.MemberIndAcct;
import org.hgvc.webapp.ets.bo.Remark;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.constant.MemberConstant;
import org.hgvc.webapp.ets.jsf.beans.admin.AdminMemberCompProfileBean;
import org.hgvc.webapp.ets.jsf.beans.admin.AdminMemberIndProfileBean;
import org.hgvc.webapp.ets.jsf.beans.admin.AdminMemberMgmtBean;
import org.hgvc.webapp.ets.jsf.beans.finance.MemberInvListingBean;
import org.hgvc.webapp.ets.spring.service.AgreementService;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.hgvc.webapp.ets.spring.service.SystemService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.DateUtil;
import org.jocsb.base.util.FacesUtils;

public class MemberAgreementTransferBean extends RegistrationBean {
	public static String BEAN_NAME = "memberAgreementTransferBean";
	public final static String NAV_LIST = "memberAgreementTransfer";
	public final static String NAV_ACTION_BULETIN = "navMemberAgreementTransferInv";

	private AgreementService agreementService;
	private MemberService memberService;
	private Member fromMember = new Member();
	private String remark = "";
	private double invoiceCharge;
	private boolean lookupAgreementForm;
	private List<Agreement> agreements;
	private Agreement agreement;
	private String status;
	private Date agreementExpiredDate;
	
	public String transferAction(){
		setMember(new Member());
		getMember().setRegistrationType(MemberConstant.MEMBER_REG_ONLINE);
		getMember().setCreatedBy(getThisUser());
		getMember().setCreatedDate(new Date());
		if(fromMember.getMemberType().equalsIgnoreCase(CommonConstant.MEMBER_TYPE_COMP)){
			AdminMemberCompProfileBean bean = (AdminMemberCompProfileBean)FacesUtils.getManagedBean(AdminMemberCompProfileBean.BEAN_NAME);
			setSelectedType(fromMember.getMemberType());
			getMember().setMemberType(CommonConstant.MEMBER_TYPE_IND);
			setMemberComp(new MemberComp()) ;
			setDependantList( new ArrayList<MemberIndAcct>());
			return "transferCompanyRegistration";
		}else{
			getMember().setMemberType(CommonConstant.MEMBER_TYPE_IND);
			setMemberInd(new MemberInd())   ;
			setDependantList( new ArrayList<MemberIndAcct>());
			setSelectedType(fromMember.getMemberType());
			AdminMemberIndProfileBean bean = (AdminMemberIndProfileBean)FacesUtils.getManagedBean(AdminMemberIndProfileBean.BEAN_NAME);
			return "transferIndividualRegistration";
		}
		
	}
	
	public String navMemberAgreementTransfer(){
		return NAV_LIST;
	}
	
	public String navMemberAgreementTransferInv(String actionNoP,String... params){
		MemberInvListingBean memberInvListingBean = (MemberInvListingBean)FacesUtils.getManagedBean(MemberInvListingBean.BEAN_NAME);
		memberInvListingBean.initFinanceRole();
		
		memberInvListingBean.setMemberSearch(params[2]);
		memberInvListingBean.listInvActionListener(params[1]);

		memberInvListingBean.searchMemberAction(null);
		memberInvListingBean.setActionNo(actionNoP);
		return memberInvListingBean.getInvLstAcOutcome();
	}
	
	public String saveMemberTransfer(){
		try{
			if(fromMember.getMemberType().equalsIgnoreCase(CommonConstant.MEMBER_TYPE_COMP)){
				memberService.saveAgreenmentTransfer( getMember(), agreement, getMemberComp(), invoiceCharge, remark);
			}else{
				memberService.saveAgreenmentTransfer( getMember(), agreement, getMemberInd(), invoiceCharge, remark);
			}
		}catch(BaseException be){
			initErrorBean(be);
		}catch(Exception be){
			initErrorBean(new BaseException(be));
		}
		
		return AdminMemberMgmtBean.NAV_LIST.concat("_"+getRoleName());
	}
	
	public Date getAgreementExpiredDate() {
		if (agreement != null) {

			if (agreement.getAgreetmentDate() != null) {
				Calendar cal = DateUtil.setTime(agreement.getAgreetmentDate());
				cal.add(Calendar.YEAR, agreement.getMembership().getMembershipTerm());
				return cal.getTime();
			}
		}
		return null;
	}

	public void setAgreementExpiredDate(Date agreementExpiredDate) {
		this.agreementExpiredDate = agreementExpiredDate;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public void ini() {
		status = "";
		lookupAgreementForm = false;
		agreement = new Agreement();
		invoiceCharge = 0;
	}

	public MemberAgreementTransferBean() {
	}

	public void lookupFormCloseActionEvent(ActionEvent event) {
		this.lookupAgreementForm = false;
	}

	public void lookupAgreementFormActionEvent(ActionEvent event) {

		this.lookupAgreementForm = true;
		try {
			List<Agreement> agreementMayExpired = memberService.getAgreementListByMemberId(fromMember.getMemberId());
			agreements = new ArrayList<Agreement>();
			for (Agreement agreement2 : agreementMayExpired) {
				Calendar cal = DateUtil.setTime(agreement2.getAgreetmentDate());
				cal.add(Calendar.YEAR, agreement2.getMembership().getMembershipTerm());
				Date expiredDate = DateUtil.formatDateByTime(cal.getTime(), 23, 59, 59, 999);

				if (expiredDate.after(new Date())) {
					agreements.add(agreement2);
				}
			}
		} catch (Exception be) {
			initErrorBean(new BaseException(be));
		}
	}
	
	public static UIComponent findComponentInRoot(String id) {
		UIComponent component = null;
		FacesContext facesContext = FacesContext.getCurrentInstance();
		if (facesContext != null) {
			UIComponent root = facesContext.getViewRoot();
			component =  findComponent(root, id);
			 
		}
		return component;
	}
	public static UIComponent findComponent(UIComponent base, String id) {
		if (id.equals(base.getId()))
			return base;

		UIComponent children = null;
		UIComponent result = null;
		Iterator childrens = base.getFacetsAndChildren();
		while (childrens.hasNext() && (result == null)) {
			children = (UIComponent) childrens.next();
			if (id.equals(children.getId())) {
				result = children;
				break;
			}
			result = findComponent(children, id);
			if (result != null) {
				break;
			}
		}
		return result;
	}
	public void lookupAgreementSelectedActionEvent(ActionEvent event) {
		this.lookupAgreementForm = false;
		UIParameter param = (UIParameter) event.getComponent().findComponent("agreementId");
		String agreementId = param.getValue().toString();
		for (Agreement agreementP : this.agreements) {
			if (agreementP.getAgreementId().equalsIgnoreCase(agreementId)) {
				this.agreement = agreementP;
				status = "Active";
				HtmlInputHidden input = (HtmlInputHidden) findComponentInRoot("agreementNo");
				input.setSubmittedValue(agreement.getAgreetmentNo());
				input.setValue(agreement.getAgreetmentNo());
				break;
			}
		}
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public double getInvoiceCharge() {
		return invoiceCharge;
	}

	public void setInvoiceCharge(double invoiceCharge) {
		this.invoiceCharge = invoiceCharge;
	}

	public void setAgreementService(AgreementService agreementService) {
		this.agreementService = agreementService;
	}

	public Member getFromMember() {
		return fromMember;
	}

	public void setFromMember(Member member) {
		this.fromMember = member;
	}

	public boolean isLookupAgreementForm() {
		return lookupAgreementForm;
	}

	public void setLookupAgreementForm(boolean lookupAgreementForm) {
		this.lookupAgreementForm = lookupAgreementForm;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public List<Agreement> getAgreements() {
		return agreements;
	}

	public void setAgreements(List<Agreement> agreements) {
		this.agreements = agreements;
	}

	public Agreement getAgreement() {
		if (agreement==null)
			agreement = new Agreement();
		return agreement;
	}

	public void setAgreement(Agreement agreement) {
		this.agreement = agreement;
	}

}
